<template>
 <div>
   <edit-form @onSubmit="loadResources ()" ref="edit"></edit-form>
   <div style="margin-top:10px;">
     <el-table
    :data="resources"
    :height="tableHeight"
    stripe
    style="width: 100%">
    <el-table-column
      prop="createTime"
      label="捐献日期"
      :formatter="dateFormat"
      width="145">
    </el-table-column>
    <el-table-column
      prop="dcode"
      label="提取码"
      width="70">
    </el-table-column>
    <el-table-column
      prop="ddesc"
      label="提取地址">
    </el-table-column>

    <el-table-column label="操作" width="100">
      <template slot-scope="scope">
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>

  </el-table>
  </div>
  <div style="text-align:center;padding:10px;">
                  <el-pagination
                background
                layout="total, prev, pager, next, jumper"
                @current-change="handleCurrentChange"
                :page-size="pageSize"
                :total="total">`
                </el-pagination>
  </div>
  <el-button type="primary" style="margin-top:20px" @click.native.prevent="addResource()">上传资源链接<i class="el-icon-upload el-icon--right"></i></el-button>
 </div>
</template>

<script>
import EditForm from '../common/EditForm'
 export default {
   name: '',
   data () {
     return {
       resources: [],
        pageSize: 6,
        total: 0,
        keywords: '',
     }
   },
   props: [],
   components: {
     EditForm
   },
    computed: {
      tableHeight () {
        return 364
      }
    },
   created () {},
   mounted () {this.loadResources()},
   methods: {
             loadResources () {
                var _this = this
                this.$axios.post('/resource/' + this.pageSize + '/1').then(resp => {
                  if (resp && resp.data.code === 200) {
                    _this.resources = resp.data.data.records
                    _this.total = resp.data.data.total
                  }
                })
              },
              handleCurrentChange (page) {
                var _this = this
                this.$axios.post('/resource/' + this.pageSize + '/' + page).then(resp => {
                  if (resp && resp.data.code === 200) {
                    _this.resources = resp.data.data.records
                    _this.total = resp.data.data.total
                  }
                })
              },
              dateFormat(row, column) {
                // 获取单元格数据
                let data = row[column.property]
                if (data ==='') {
                    return ''
                }else {
                    let dt = new Date(data)
                    return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
                }
              },
               handleDelete(id) {
                this.$confirm('你确定要取消这个资源的捐赠, 是否继续?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                    this.$axios
                      .delete('/resource/delete/' + id).then(resp => {
                      if (resp && resp.data.code === 200) {
                        this.loadResources ()
                      }
                    })
                  }
                ).catch(() => {
                  this.$message({
                    type: 'info',
                    message: '已取消删除'
                  })
                })
              },
              addResource(){
                this.$refs.edit.dialogFormVisible = true
              }

   }
 }
</script>

<style lang="css" scoped>


</style>
